package main

import (
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"net/url"
)

func main() {

	e := echo.New()
	e.Use(middleware.ProxyWithConfig(CustomProxyConfig()))

	e.Start(":8080")
}

func CustomProxyConfig() middleware.ProxyConfig {
	return middleware.ProxyConfig{
		Skipper: func(c echo.Context) bool {
			return false
		},
		Balancer: GenRandomBalancer(),
		Rewrite:  map[string]string{},
	}
}

// GenRandomBalancer
//
//	@Description: 随机负载均衡
//	@return middleware.ProxyBalancer
func GenRandomBalancer() middleware.ProxyBalancer {
	urlOne, _ := url.Parse("http://localhost:8081")
	urlTwo, _ := url.Parse("http://localhost:8082")
	m := middleware.NewRandomBalancer([]*middleware.ProxyTarget{ //随机负载均衡
		{
			URL: urlOne,
		},
		{
			URL: urlTwo,
		},
	})
	return m
}

// GenNewRoundRobinBalancer
//
//	@Description: 轮询负载均衡
//	@return middleware.ProxyBalancer
func GenNewRoundRobinBalancer() middleware.ProxyBalancer {
	urlOne, _ := url.Parse("http://localhost:8081")
	urlTwo, _ := url.Parse("http://localhost:8082")
	m := middleware.NewRoundRobinBalancer([]*middleware.ProxyTarget{ //随机负载均衡
		{
			URL: urlOne,
		},
		{
			URL: urlTwo,
		},
	})
	return m
}
